package cn.edu.xmu.oomall.order.controller;

import cn.edu.xmu.javaee.core.aop.Audit;
import cn.edu.xmu.javaee.core.aop.LoginUser;
import cn.edu.xmu.javaee.core.exception.BusinessException;
import cn.edu.xmu.javaee.core.model.Constants;
import cn.edu.xmu.javaee.core.model.ReturnNo;
import cn.edu.xmu.javaee.core.model.ReturnObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.order.controller.dto.OrderItemDto;
import cn.edu.xmu.oomall.order.controller.vo.OrderItemVo;
import cn.edu.xmu.oomall.order.dao.bo.OrderItem;
import cn.edu.xmu.oomall.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import cn.edu.xmu.javaee.core.util.CloneFactory;
import org.springframework.web.bind.annotation.*;

/**
 * @author 谢传艳
 * @date 2024/12/13
 */
@RestController /*Restful的Controller对象*/
@RequestMapping(produces = "application/json;charset=UTF-8")
@RequiredArgsConstructor
public class OrderController {
    private final OrderService orderService;

    /**
     * 根据id查找订单明细
     * @param oid 订单明细id
     * @return 订单明细对象
     */
    @Audit(departName = "orderItem")
    @GetMapping("/orderitems/{oid}")
    public ReturnObject findOrderItemById(@PathVariable Long oid) {
        OrderItem orderItem = orderService.findOrderItemById(oid);
        return new ReturnObject(ReturnNo.OK, CloneFactory.copy(new OrderItemVo(orderItem), orderItem));
    }

    /**
     * 修改订单明细
     * @param oid 订单明细id
     * @param dto 订单明细信息
     * @param user 用户
     * @return 订单明细对象
     */
    @Audit(departName = "orderItem")
    @PutMapping("/orderitems/{oid}")
    public ReturnObject modifyOrderItem(@PathVariable Long oid, @RequestBody OrderItemDto dto,
                                        @LoginUser UserDto user) {
        OrderItem orderItem = CloneFactory.copy(new OrderItem(), dto);
        orderItem.setId(oid);
        OrderItem newOrderItem = orderService.modifyOrderItem(orderItem, user);
        return new ReturnObject(ReturnNo.OK, CloneFactory.copy(new OrderItemVo(newOrderItem), newOrderItem));
    }
}
